﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Cooperation.WebsiteCooperation;
using BCDto.Common.Cooperation.WebsiteCooperation;
using BCEntity.Common.Cooperation.WebsiteCooperation;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.Cooperation.WebsiteCooperation
{
    public class WebsiteCooperationService : IWebsiteCooperationService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteCooperationData websiteCooperationData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteCooperationEntity);

        public WebsiteCooperationService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteCooperationData websiteCooperationData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteCooperationData = websiteCooperationData;
        }

        #region 检查官网合作留言是否存在
        public bool Exists(int websiteCooperationId)
        {
            return this.websiteCooperationData.Exists(websiteCooperationId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网合作留言
        public WebsiteCooperationDto Add(WebsiteCooperationRequestDto requestDto)
        {
            if(this.websiteCooperationData.Exists(requestDto.Company).Result)
            {
                throw new ArgumentException("您已提交申请，稍后我公司会与您取得联系");
            }

            var entity = requestDto.As<WebsiteCooperationEntity>();

            return this.websiteCooperationData.Add(entity).GetAwaiter().GetResult().As<WebsiteCooperationDto>();
        }
        #endregion

        #region 修改官网合作留言
        public WebsiteCooperationDto Update(WebsiteCooperationPutDto putDto)
        {
            if (!this.Exists(putDto.WebsiteCooperationId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<WebsiteCooperationEntity>();
            var result = this.websiteCooperationData.Update(entity).GetAwaiter().GetResult().As<WebsiteCooperationDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteCooperationId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网合作留言
        public bool Delete(int websiteCooperationId)
        {
            if (!this.Exists(websiteCooperationId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteCooperationData.Delete(websiteCooperationId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteCooperationId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网合作留言
        public WebsiteCooperationDto Get(int websiteCooperationId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteCooperationId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCooperationData.GetEntity(websiteCooperationId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteCooperationDto>();
        }
        #endregion

        #region 获取官网合作留言列表
        public IEnumerable<WebsiteCooperationDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.websiteCooperationData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteCooperationDto>>();
        }
        #endregion

        #region 获取官网合作留言分页
        public PageableList<WebsiteCooperationDto> Query(bool? hasFollowUp, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, hasFollowUp, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteCooperationData.Query(hasFollowUp,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteCooperationDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteCooperationDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}